package com.dodo.videoEdit.ui.fragment;

import androidx.lifecycle.Observer;
import androidx.lifecycle.ViewModelProviders;

import com.azhon.basic.adapter.OnItemClickListener;
import com.azhon.basic.base.BaseLazyFragment;
import com.dodo.videoEdit.R;
import com.dodo.videoEdit.databinding.FragmentFileChoseBinding;
import com.dodo.videoEdit.greendao.FileDirType;
import com.dodo.videoEdit.ui.filechoseactivity.FileChoseViewModel;
import com.dodo.videoEdit.ui.fragment.adapters.FoldingCellListAdapter;

import java.util.ArrayList;
import java.util.List;
import java.util.Objects;


public class FileChoseChildFragment extends BaseLazyFragment<FileChoseViewModel, FragmentFileChoseBinding> implements OnItemClickListener<FileDirType> {

    private String title;
    private int type;
    private List<FileDirType> data = new ArrayList<>();
    private FoldingCellListAdapter adapter1;

    public static FileChoseChildFragment newInstance(String title, int type) {
        FileChoseChildFragment sf = new FileChoseChildFragment();
        sf.title = title;
        sf.type = type;
        return sf;
    }

    @Override
    protected FileChoseViewModel initViewModel() {
        return ViewModelProviders.of(Objects.requireNonNull(getActivity())).get(FileChoseViewModel.class);
    }

    @Override
    protected void showError(Object obj) {

    }

    @Override
    protected int onCreate() {
        return R.layout.fragment_file_chose;
    }

    @Override
    protected void initView() {
       /* RecyclerView rvNews = dataBinding.fragmentFileChoseRv;
        LinearLayoutManager manager = new LinearLayoutManager(context);
        manager.setOrientation(LinearLayoutManager.VERTICAL);
        rvNews.setLayoutManager(manager);
        adapter = new FileChoseItemAdapter2(type);
        //adapter.setOnItemListener(this);
        rvNews.setAdapter(adapter);*/
        adapter1 = new FoldingCellListAdapter(getContext(), data,type);
            dataBinding.fragmentFileChoseRv.setAdapter(adapter1);
    }

    @Override
    protected void initData() {
        switch (type) {
            case 1:
                viewModel.getAudioDir().observeForever(new Observer<List<FileDirType>>() {
                    @Override
                    public void onChanged(List<FileDirType> fileDirTypes) {
                       // adapter.setNewData(fileDirTypes);
                        data.clear();
                        data.addAll(fileDirTypes);
                        adapter1.notifyDataSetChanged();
                    }
                });
                break;
            case 2 :
                viewModel.getImageDir().observeForever(new Observer<List<FileDirType>>() {
                    @Override
                    public void onChanged(List<FileDirType> fileDirTypes) {
                        data.clear();
                        data.addAll(fileDirTypes);
                        adapter1.notifyDataSetChanged();
                    }
                });
                break;
            case 3 :
                viewModel.getVideoDir().observeForever(new Observer<List<FileDirType>>() {
                    @Override
                    public void onChanged(List<FileDirType> fileDirTypes) {
                        data.clear();
                        data.addAll(fileDirTypes);
                        adapter1.notifyDataSetChanged();
                    }
                });
                break;
        }
    }

    @Override
    public void onItemClick(FileDirType fileDirType, int position) {

    }

    @Override
    public boolean onItemLongClick(FileDirType fileDirType, int position) {
        return false;
    }

    @Override
    protected void lazyLoad() {
        switch (type) {
            case 1:
                viewModel.findAudioDir();
                break;
            case 2 :
                viewModel.findImageDir();
                break;
            case 3 :
                viewModel.findVideoDir();
                break;
        }
    }




}
